import Mock from "mockjs";

// 创建数据
let { data } = Mock.mock({
  // 20条数据
  "data|100": [
    {
      linkman: "@cname",
      "sex|1": ["男", "女"],
      "phone|13000000000-18999999999": 13000000000,
      "number|1577836800000-1670830434813": 1577836800000,
      "goodsAddress|1": [
        "绿城留香园",
        "绿城丽江公寓（北区）",
        "滨江金色黎明（三期）",
      ],
      notvip: "非会员",
      "money|10-10000": 10,
    },
  ],
});

//列表接口 分页 搜索
Mock.mock("/api/nomember/list", "post", (options) => {
  const { pageSize, currentPage, search } = JSON.parse(options.body);

  if (pageSize && currentPage) {
    let result;
    let count = 0;
    if (search) {
      result = data
        .filter(
          (item: any) =>
            item.linkman == search ||
            item.phone == search ||
            item.number == search
        )
        .slice((currentPage - 1) * pageSize, currentPage * pageSize);
      count = data.filter(
        (item: any) =>
          item.linkman == search ||
          item.phone == search ||
          item.number == search
      ).length;
    } else {
      result = data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
      count = data.length;
    }
    return {
      status: 0,
      result: result,
      count: count,
    };
  } else {
    return {
      status: 1,
      result: "参数错误",
    };
  }
});

//删除接口
Mock.mock("/api/nomember/del", "post", (options) => {
  const { phone } = JSON.parse(options.body);
  if (phone) {
    data = data.filter((item: any) => item.phone != phone);
    return {
      status: 0,
      msg: "删除成功",
    };
  } else {
    return {
      status: 1,
      msg: "参数错误",
    };
  }
});

export default Mock;
